﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using C1.Win.C1Ribbon;
using C1.Win.C1Tile;
using System.Xml;
namespace TC4T_WinForm
{
    public partial class MainForm : C1.Win.C1Ribbon.C1RibbonForm
    {
        private TCForm activeForm;
        private TCViewer viewer = null;
        public static MainForm Form
        {
            get;
            set;
        }
        public MainForm()
        {
            MyTCSession.InitSession();
            InitializeComponent();
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
            comboVisualStyles.SelectedOption = office2010BlueOption;
            
            viewer = new TCViewer();
            Form = this;
        }
        private void inputComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboVisualStyles.SelectedOption.Text.Equals("Office 2007 Blue"))
            {
                TC4T_WinForm.Properties.Settings.Default.VisualStyle = C1.Win.C1Ribbon.VisualStyle.Office2007Blue;
                c1SplitContainer1.ForeColor = Color.Black;
                c1SplitContainer1.VisualStyle = C1.Win.C1SplitContainer.VisualStyle.Office2007Blue;
            }
            else if (comboVisualStyles.SelectedOption.Text.Equals("Office 2007 Silver"))
            {
                TC4T_WinForm.Properties.Settings.Default.VisualStyle = C1.Win.C1Ribbon.VisualStyle.Office2007Silver;
                c1SplitContainer1.ForeColor = Color.Black;
                //c1TileControl1.TileBackColor = Color.DimGray;
                //c1TileControl1.TileForeColor = Color.White;
                c1SplitContainer1.VisualStyle = C1.Win.C1SplitContainer.VisualStyle.Office2007Silver;
            }
            else if (comboVisualStyles.SelectedOption.Text.Equals("Office 2007 Black"))
            {
                TC4T_WinForm.Properties.Settings.Default.VisualStyle = C1.Win.C1Ribbon.VisualStyle.Office2007Black;
                c1SplitContainer1.ForeColor = Color.White;
                //c1TileControl1.TileBackColor = Color.DarkGray;
                //c1TileControl1.TileForeColor = Color.White;
                c1SplitContainer1.VisualStyle = C1.Win.C1SplitContainer.VisualStyle.Office2007Black;
            }
            else if (comboVisualStyles.SelectedOption.Text.Equals("Office 2010 Blue"))
            {
                TC4T_WinForm.Properties.Settings.Default.VisualStyle = C1.Win.C1Ribbon.VisualStyle.Office2010Blue;
                c1SplitContainer1.ForeColor = Color.Black;
                //c1TileControl1.TileBackColor = Color.Navy;
                //c1TileControl1.TileForeColor = Color.White;
                c1SplitContainer1.VisualStyle = C1.Win.C1SplitContainer.VisualStyle.Office2010Blue;
            }
            else if (comboVisualStyles.SelectedOption.Text.Equals("Office 2010 Silver"))
            {
                TC4T_WinForm.Properties.Settings.Default.VisualStyle = C1.Win.C1Ribbon.VisualStyle.Office2010Silver;
                c1SplitContainer1.ForeColor = Color.Black;
                //c1TileControl1.TileBackColor = Color.DimGray;
                //c1TileControl1.TileForeColor = Color.White;
                c1SplitContainer1.VisualStyle = C1.Win.C1SplitContainer.VisualStyle.Office2010Silver;
            }
            else if (comboVisualStyles.SelectedOption.Text.Equals("Office 2010 Black"))
            {
                TC4T_WinForm.Properties.Settings.Default.VisualStyle = C1.Win.C1Ribbon.VisualStyle.Office2010Black;
                c1SplitContainer1.ForeColor = Color.White;
                //c1TileControl1.TileBackColor = Color.Gainsboro;
                //c1TileControl1.TileForeColor = Color.Black;
                c1SplitContainer1.VisualStyle = C1.Win.C1SplitContainer.VisualStyle.Office2010Black;
            }
            else
            {
                TC4T_WinForm.Properties.Settings.Default.VisualStyle = C1.Win.C1Ribbon.VisualStyle.Windows7;
                c1SplitContainer1.ForeColor = Color.Black;
                //c1TileControl1.TileBackColor = Color.DarkGray;
                //c1TileControl1.TileForeColor = Color.White;
                c1SplitContainer1.VisualStyle = C1.Win.C1SplitContainer.VisualStyle.Windows7;
            }
            UpdateVisualStyle();
        }
        internal void UpdateVisualStyle()
        {
            if (activeForm != null) // ControlExplorerVisualStyle can be removed if all controls can catch ambient VisualStyle from this.VisualStyle value
            {
                activeForm.ControlExplorerVisualStyle = TC4T_WinForm.Properties.Settings.Default.VisualStyle;
            }
            this.VisualStyle = TC4T_WinForm.Properties.Settings.Default.VisualStyle;
            OnVisualStyleChanged(EventArgs.Empty); // force updating ambient VisualStyle property in all child controls
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            //Create an XmlReader to begin parsing our xml
            XmlReader xr = XmlReader.Create(@"config\C1Controls.xml");

            int pageIndex = 0;
            c1TileControl1.ImageList = controlIcons;
            while (xr.Read())
            {
                if (xr.NodeType == XmlNodeType.Element)
                {
                    switch (xr.Name)
                    {
                        case "control":
                            string name = xr.GetAttribute("name");
                            C1.Win.C1Tile.Tile t1 = new C1.Win.C1Tile.Tile();
                            t1.Text = name;
                            t1.ImageIndex = pageIndex;
                            c1TileControl1.Groups[0].Tiles.Add(t1);

                            if (pageIndex == 0)
                            {
                                t1.ImageIndex = 0;

                            }
                            pageIndex++;
                            C1.Win.C1Tile.Group g1 = new C1.Win.C1Tile.Group();
                            g1.Visible = false;
                            g1.Name = name;
                            g1.Text = name;
                            c1TileControl1.Groups.Add(g1);
                            gname = name;
                            break;

                        case "sample":
                            C1.Win.C1Tile.Tile tsample = new C1.Win.C1Tile.Tile();
                            tsample.Text = xr.GetAttribute("name");
                            if (c1TileControl1.Groups[pageIndex].Name == "What's New")
                            {
                                if (tsample.Text == "Home")
                                {
                                    tsample.Image = sampleIcons.Images[0];
                                }
                                else
                                {
                                    tsample.Image = sampleIcons.Images[2];
                                }
                            }
                            else
                            {
                                tsample.Image = sampleIcons.Images[1];
                            }
                            c1TileControl1.Groups[pageIndex].Tiles.Add(tsample);

                            tsample.Tag = new TCFormClass(xr.GetAttribute("form"), xr.GetAttribute("description"));

                            break;
                    }
                }
            }

            LoadWelcomePage();
            comboVisualStyles.SelectedIndex = 4;
            
        }
        private void AddOrRemove(Control.ControlCollection collection, Control control)
        {
            if (control == null)
            {
                collection.Clear();
                return;
            }

            if (!collection.Contains(control))
            {
                collection.Clear();
                collection.Add(control);
                control.Dock = DockStyle.Fill;
                control.Visible = true;
            }
        }

        public void Attach(Control viewer)
        {
            AddOrRemove(panel1.Controls, viewer);
        }

        public void Detach()
        {
            Attach(null);
        }

        void ResetViewer()
        {
            viewer.Close();
        }

        private void LoadWelcomePage()
        {
            activeForm = viewer.Run(typeof(HomePage).ToString());
            this.Attach(viewer);
            RefreshTiles();
        }
        private void RefreshTiles()
        {
            c1TileControl1.Text = "Controls";
            foreach (C1.Win.C1Tile.Group g1 in c1TileControl1.Groups)
            {
                g1.Visible = false;
            }
            c1TileControl1.Groups[0].Visible = true;
        }

        private void c1PictureBox1_Click(object sender, EventArgs e)
        {
            LoadWelcomePage();
        }

        public void LoadPage(string formName)
        {
            this.Cursor = Cursors.AppStarting;
            activeForm = viewer.Run(formName);
            this.Attach(viewer);
            this.Cursor = Cursors.Default;
        }

        string gname;//单机每个瓷片的名称
        private void c1TileControl1_TileClicked(object sender, TileEventArgs e)
        {

            gname = e.Tile.Text;
            if (e.Tile.Group.Index == 0)
            {
                c1TileControl1.Text = gname;
            }
            else
            {
                c1TileControl1.Text = e.Tile.Group.Name;
            }

            foreach (C1.Win.C1Tile.Group g1 in c1TileControl1.Groups)
            {
                if (g1.Name == gname)
                {
                    g1.Visible = true;
                    c1TileControl1.Groups[0].Visible = false;
                }
            }
            if (e.Tile.Group.Index != 0)
            {
                TCFormClass d = (TCFormClass)e.Tile.Tag;
                this.Cursor = Cursors.AppStarting;
                activeForm = viewer.Run(d.FormName);
                this.Attach(viewer);
                this.Cursor = Cursors.Default;
            }
        }
    }

    public class TCFormClass
    {
        string _form;
        string _desc;
        string _name;

        public TCFormClass(string form, string desc)
        {
            _form = form;
            _desc = desc;
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public string FormName
        {
            get { return _form; }
            set { _form = value; }
        }

        public string Description
        {
            get { return _desc; }
            set { _desc = value; }
        }

    }
}
